/******************************************************************************
 *  Include Files
 *****************************************************************************/
#include "DSM.h"

/******************************************************************************
 *  Macro Definitions
 *****************************************************************************/
#define DSM_DFC_NUM 8

/******************************************************************************
 *  Const Definitions
 *****************************************************************************/

/*****************************************************************************
 *  Global Variables Definitions
 *****************************************************************************/
uint32_t DSM_FaultState_Boardcast_RVSB = 0;
uint32_t DSM_FaultState_Boardcast_IRRV = 0;
static DSM_DFC *DSM_DFC_List[DSM_DFC_NUM] = {NULL};
uint8_t DSM_stErrReactn[DSM_ErrReactn_SUM] = {0};

/*****************************************************************************
 *  Local Function declarations
 *****************************************************************************/
uint32_t SET_BIT32(uint32_t var, uint8_t offset);
void DSM_Trigger(DSM_DFC *DFC, uint8_t OK_NG);
void DSM_DFC_List_Trigger(DSM_DFC *DFC);

/*****************************************************************************
 *  Function Definitions
 *****************************************************************************/
void DSM_Trigger(DSM_DFC *DFC, uint8_t OK_NG) {
  if (1 == OK_NG) {
    if (0 == DFC->cntrDeb) {
      DFC->stFault[DSM_FaultType_RVSB] = 0;
    } else {
      DFC->cntrDeb -= 1;
    }
  } else {
    DFC->cntrDeb += 2;
    if (DFC->cntrDeb >= DFC->cntrDebMax_C) {
      DFC->cntrDeb = DFC->cntrDebMax_C;
      DFC->stFault[DSM_FaultType_RVSB] = 1;
      DFC->stFault[DSM_FaultType_IRRV] = 1;
      DSM_DFC_List_Trigger(DFC);
    }
  }
}

void DSM_DFC_List_Trigger(DSM_DFC *DFC) {
  static uint8_t ListNum = 0;
  if (0 == DFC->stAdded_DFC_List) {
    DFC->stAdded_DFC_List = 1;
    DSM_DFC_List[ListNum] = DFC;
    ListNum++;
    if (ListNum >= DSM_DFC_NUM) {
      ListNum = 0;
    }
  }
}

void DSM_ErrReactn_Main(void) {
  uint8_t i = 0;
  uint8_t temp_stErrReactn[DSM_ErrReactn_SUM] = {0};
  uint32_t temp_FaultState_Boardcast_RVSB = 0;
  uint32_t temp_FaultState_Boardcast_IRRV = 0;
  for (i = 0; i < DSM_DFC_NUM; i++) {
    if (NULL != DSM_DFC_List[i]) {
      if (0 != DSM_DFC_List[i]->stFault[DSM_DFC_List[i]->stFault_Type_C]) {
        if (temp_stErrReactn[DSM_DFC_List[i]->stFault_ErrReactn_C] <
            DSM_DFC_List[i]->stFault_Type_C) {
          temp_stErrReactn[DSM_DFC_List[i]->stFault_ErrReactn_C] =
              DSM_DFC_List[i]->stFault_Type_C;
        }
      }
      if (0 != DSM_DFC_List[i]->stFault[DSM_FaultType_RVSB]) {
        temp_FaultState_Boardcast_RVSB =
            SET_BIT32(temp_FaultState_Boardcast_RVSB, DSM_DFC_List[i]->nNo_C);
      }
      if (0 != DSM_DFC_List[i]->stFault[DSM_FaultType_IRRV]) {
        temp_FaultState_Boardcast_IRRV =
            SET_BIT32(temp_FaultState_Boardcast_IRRV, DSM_DFC_List[i]->nNo_C);
      }
    }
  }
  for (i = 0; i < DSM_ErrReactn_SUM; i++) {
    DSM_stErrReactn[i] = temp_stErrReactn[i];
  }
  DSM_FaultState_Boardcast_RVSB = temp_FaultState_Boardcast_RVSB;
  DSM_FaultState_Boardcast_IRRV = temp_FaultState_Boardcast_IRRV;
}

inline uint32_t SET_BIT32(uint32_t var, uint8_t offset) {
  return var |= (1U << offset);
}
